﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Net;
using System.Net.Sockets;


//智慧通報機器人-確認發送群組警告訊息
//Creator：Rita Lin
//Create Time：2014-05-19
//Update Time：2014-07-14

namespace CCPSIP.DAuth
{
  public partial class warning : System.Web.UI.Page
  {
    string str_eventId = "";
    string str_sipNo = "";
    private const string remoteIP = "192.168.209.4";//Socket通報IP
    private const int remotePort = 13000;           //Socket通報Port


    protected void Page_Load(object sender, EventArgs e) {
      str_eventId = "";
      str_sipNo = "";

      try {
        //【01】取得通報訊息編號及通報者SIPNO
        //
        str_eventId = Request.Params["eventId"];
        str_sipNo = Request.Params["sipNo"];
        string idHeader = Request.Form["idHeader"];
        string checkcode = Request.Form["checkcode"];


        //【01-2】驗證身分
        //
        SipUserInfo userInfo = SipKit.GetUserInfo(idHeader, checkcode, Request);
        if (userInfo == null) {
          throw new Exception("無法識別您的身分");
        }
        str_sipNo = userInfo.SIP_Num;
        lblUserHello.Text = string.Format("{0} 您好，", userInfo.CustomName);


        if (!string.IsNullOrEmpty(str_sipNo)) {
          if (str_eventId.Length == 17) {
            DateTime date = DateTime.ParseExact(str_eventId.Substring(0, 14), "yyyyMMddHHmmss", null);

            if (DateTime.Now > date.AddMinutes(10)) {
              hideBtn("通報時效過期，系統已自動發送!");
            }
            else {
              lbl_eventId.Text = "<h3>No." + str_eventId + "</h3>"
                + Environment.NewLine + "<h3>1.請與現場聯繫確認</h3>"
                + Environment.NewLine + "<h3>2.確定為火焰警報請立即通報</h3>";
            }
          }
          else
            hideBtn("通報事件編號不存在!");
        }
        else
          hideBtn("您未有通報權限!");
      }
      catch (Exception ex) {
        string errorMsg = string.Format("Error: {0}", ex.Message);
        hideBtn(errorMsg);
      }
    }


    //【02】發送群組通報訊息Socket YES
    protected void Send_Yes_Click(object sender, EventArgs e) {
      Connect(str_eventId + ",Y," + str_sipNo, "已發送訊息至通報群組!");
    }


    //【03】發送群組通報訊息Socket NO
    protected void Send_No_Click(object sender, EventArgs e) {
      Connect(str_eventId + ",N," + str_sipNo, "已標示為非緊急事件!");
    }


    //【04】隱藏通報按鈕
    protected void hideBtn(string txtMsg) {
      lbl_eventId.Text = "<h3>" + txtMsg + "</h3>";
      btn_Send_No.Visible = false;
      btn_Send_Yes.Visible = false;
    }


    //【05】傳送Socket
    private void Connect(string strMsg, string strBtn) {
      IPAddress ipAddress = IPAddress.Parse(remoteIP);
      IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, remotePort);
      Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

      try {
        clientSocket.Connect(ipEndPoint);
        byte[] bytes = System.Text.Encoding.UTF8.GetBytes(strMsg);
        clientSocket.Send(bytes);
        hideBtn(strBtn);
      }
      catch (Exception e) {
        hideBtn("通報發送失敗!" + e.ToString());
      }
      finally {
        clientSocket.Disconnect(false);
        clientSocket.Close();
      }
    }


  }
}
